package com.student.wym.controller;

import com.student.wym.po.Answer;
import com.student.wym.po.Question;
import com.student.wym.po.Student;
import com.student.wym.service.IAnswerService;
import com.student.wym.service.IQuestionService;
import com.student.wym.util.JSONResult;
import com.student.wym.util.PageList;
import com.student.wym.vo.AnswerRequestVo;
import com.student.wym.vo.AnswerRespVo;
import com.student.wym.vo.AnswerStudentInfoVo;
import com.student.wym.vo.AnswerVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/answer")
public class AnswerController {

    @Autowired
    private IAnswerService answerService;

    @Autowired
    private IQuestionService questionService;

    @RequestMapping(value = "/goPage", method = RequestMethod.GET)
    public String goPage() {
        return "list_answer";
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public PageList list(AnswerRequestVo reqVo, HttpServletRequest req, HttpServletResponse resp) {
        PageList result = new PageList();
        if (StringUtils.isEmpty(reqVo.getKeyValue())) {
            return result;
        } else {
            AnswerRespVo respVo = answerService.findOneByKey(reqVo);
            List<Answer> answers = respVo.getAnswers();
            List<AnswerVo> answerVos = new ArrayList<AnswerVo>();
            if (CollectionUtils.isNotEmpty(answers)) {
                for (int i = 0, len = answers.size(); i < len; ++i) {
                    AnswerVo vo = new AnswerVo();
                    vo.setCode(answers.get(i).getQuestionId());
                    vo.setResule(answers.get(i).getResult());
                    vo.setErrorResult(answers.get(i).getAnswer());
                    Question question = questionService.findById(answers.get(i).getQuestionId());
                    vo.setIntro(question.getIntro());
                    vo.setType(question.getType());
                    answerVos.add(vo);
                }
            }
            result.setRows(answerVos);
            result.setTotal(answerVos.size());
            return result;
        }
    }

    @RequestMapping("/getStudentInfo")
    @ResponseBody
    public JSONResult getStudentInfo(HttpServletRequest req) {
        String studentNo = req.getParameter("studentInfoNo");
        String studentName = req.getParameter("studentInfoName");
        Student student = null;
        List<Student> students = null;
        if (StringUtils.isNotBlank(studentNo)) {
            student = answerService.findStudentByNo(studentNo);
        } else {
            students = answerService.findStudentByName(studentName);
            if (CollectionUtils.isNotEmpty(students)) {
                student = students.get(0);
            }
        }
        AnswerStudentInfoVo failStudent = new AnswerStudentInfoVo();
        if (student == null) {
            return new JSONResult(1, "暂无数据");
        } else {
            if (CollectionUtils.isNotEmpty(students) && students.size() > 1) {
                failStudent.setName(student.getName() + "（有重名考生，展示最近时间的考生，建议重名考生以学号查询）");
            } else {
                failStudent.setName(student.getName());
            }
            if ((student.getExamState() == 2 || student.getExamState() == 3) && student.getMakeUpState() == 0) {
                failStudent.setScore(student.getExamScore() + "");
            } else if (student.getExamState() == 3 && (student.getMakeUpState() == 2 || student.getMakeUpState() == 3)) {
                failStudent.setScore(student.getMakeUpScore() + "");
            } else {
                failStudent.setScore("未考试或正在考试，暂无分数");
            }
            failStudent.setThreeInfo(student.getGrade() + "" + student.getDepartment() + "" + student.getMajor());
            return new JSONResult(failStudent);
        }
    }

    @RequestMapping("/lookChoice")
    @ResponseBody
    public JSONResult lookChoice(HttpServletRequest req) {
        String id = req.getParameter("questionId");
        if (StringUtils.isNotEmpty(id)) {
            Question question = questionService.findById(Integer.parseInt(id));
            List<String> resultList = new ArrayList<String>();
            resultList.add("A、" + question.getOptionA());
            resultList.add("B、" + question.getOptionB());
            resultList.add("C、" + question.getOptionC());
            resultList.add("D、" + question.getOptionD());
            return new JSONResult(resultList);
        } else {
            return new JSONResult(1, "无数据");
        }
    }

}
